/*
 *说明：机构信息表 首页脚本
 *
 *@author liuxun 2012-03-06
*/

var opeForm;
$(function() {
	
	opeForm = $('#opeForm');
	hitTree("0");
	$("#addBtn").click(addOperation);
	$('#editBtn').click(editOperation);
	$("#deleteBtn").click(deleteOperation);
	$("#reloadBtn").click(refreshWin);
	
});

// 新增机构信息操作
function addOperation() {
	var selectedNode = treeFrame.getSelecdedNode();
	
	if (!selectedNode) {
		showInfoWin('请先选中节点!');
		return;
	}
	
	treeFrame.expandNode();
	
	var selectedNodeCode;
	if (selectedNode.id == '0') {
		selectedNodeCode = '';
	} else {
		selectedNodeCode = selectedNode.attributes.org_code;
	}
	
	var url = 'A_ORG!showAdd.do?model.org_code=' + selectedNodeCode;
	openWin(url, 'pop');
}

//编辑机构信息操作
function editOperation() {
	var selectedNode = treeFrame.getSelecdedNode();
	
	if (!selectedNode) {
		showInfoWin('请先选中节点!');
		return;
	}
	
	if (selectedNode.id == '0') {
		showInfoWin('不允许修改根节点!');
		return;
	}
	
	var selectNodeKey = selectedNode.attributes.org_id;
	var url = 'A_ORG!showEdit.do?model.key=' + selectNodeKey;
	openWin(url, 'pop');
}

// 删除机构信息操作
function deleteOperation() {
	var seletedNode = treeFrame.getSelecdedNode();
	if (seletedNode == null) {
		showInfoWin("请先选择要删除的记录！");
		return;
	}
	
	if(seletedNode.id == '0'){
		showInfoWin('不允许删除根节点!');
		return;
	}
	
	if(seletedNode.attributes.is_leaf != '1'){
		showInfoWin('非低级机构，不允许删除！');
		return;
	}
	
	var selectedNodeKey = seletedNode.attributes.org_id;
	$.messager.confirm('提示信息', '您确定要删除选择的机构信息吗?', function(result) {
		if (result) {
			showSystemProccessing();
			$.ajax( {
				url : 'A_ORG!doDelete.do',
				data : {
					"id" : selectedNodeKey
				},
				type : 'post',
				success : function(data) {
					removeSystemProccessing();
					showInfoWin("操作成功！");
					treeFrame.deleteNode(seletedNode);
				}
			});
		}
	});
}

// 处理左侧树的点击事件
function hitTree(code, txt, deep, isleaf, attributes) {
	var url;
	if (code == '0') {
		url = 'A_ORG!showInfo.do';
	
	} else { 
		url = 'A_ORG!showInfo.do?model.key=' + attributes.org_id;
	}
	$('#mainFrame').attr('src', url);
}

function afterOK(operationType, data) {
	// 根据操作类型，调用添加节点或更新节点操作
	if (operationType  == '0') {
		refreshWin();
		//treeFrame.createNode(data.org_code, data.org_code + ' ' + data.org_name, data);

	} else if (operationType == '1') {
		treeFrame.updateNode(data.org_code, data.org_code + ' ' + data.org_name, data);
	}
}